//
//  AdaptiveHeight.m
//  cn.zhikao.com
//
//  Created by WJ on 16/11/1.
//  Copyright © 2016年 WJ. All rights reserved.
//

#import "AdaptiveHeight.h"

@implementation AdaptiveHeight

// 带行间距的自适应高度
+ (CGFloat)adaptiveHeightLineSpaceString:(UILabel *)labelString sizeWidth:(CGFloat)sizeWidth lineSpace:(CGFloat)lineSpace font:(CGFloat)font {
    labelString.font = [UIFont systemFontOfSize:font];
    labelString.numberOfLines = 0;
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:labelString.text];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:lineSpace];
    [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelString.text length])];
    labelString.attributedText = attributedString;

    CGSize size = CGSizeMake(sizeWidth, 20000);
    NSDictionary *attriDict = @{NSFontAttributeName:[UIFont systemFontOfSize:font],NSParagraphStyleAttributeName:paragraphStyle};
    CGRect rect = [labelString.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine attributes:attriDict context:nil];
    
    return rect.size.height;
    
}

// 不带行间距的自适应高度
+ (CGFloat)adaptiveHeightString:(UILabel *)labelString sizeWidth:(CGFloat)sizeWidth font:(CGFloat)font {
    
    CGSize size = CGSizeMake(sizeWidth, 20000);
    labelString.numberOfLines = 0;
    NSDictionary *attriDict = @{NSFontAttributeName:[UIFont systemFontOfSize:font]};
    CGRect rect = [labelString.text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading | NSStringDrawingTruncatesLastVisibleLine attributes:attriDict context:nil];
    
    return rect.size.height + 10;

}
@end
